/**
 * 二维码活动管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：二维码活动管理
     创建日期：2015-10-12
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	showAgentList();
});

/**
 * 查询代理人列表
 * @returns
 */
var showAgentList = function(){
	
	var headPath=phpHeadPath;
	var number=$("#s_number").val();
	var agentName=$("#s_name").val();
	var address=$("#s_address").val();
	
	 var datas =  {"p":"agentManageOperate","m":"query","number":number,"agentName":agentName,"address":address};
	 var columnDefine = {"id":"ID","number":"活动编号","name":"活动地点名称","address":"详细地址","qrcode":"二维码","type":"代理商类型","newUser":"新增用户数"};
	 
	 var formatters={
	            "qrcode": function (column, row)
	            {
	            	if(row.qrcode==''||row.qrcode==null){
	        			return "";
	        		}
	        		return '<a style="width:50px;height:50px;display:block;background:url('+headPath+'/'+
				        		row.qrcode+') no-repeat;background-size:50px 50px; " href="javascript:agent.showBigQrCode(this,\''+
				        		row.qrcode+'\')"></a>';
	            },
				 "type": function (column, row)
				 {
			        	if(row.type==1)
			        	{
			        		return "推广用户";
			        	}
			        	else if(row.type==2)
			        	{
			        		return "推广快递员";
			        	}
			        	else
			        	{
			        		return "";
			        	}
				 }
	 };
	 var buttons =''; //操作按钮
		buttons+='<p>';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="showQrCodeUser()">查看活动用户</button>&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-primary\' onclick="showModifyWin();">编辑</button>&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-danger\' onclick="createQrCode();">生成二维码</button>&nbsp;';
		buttons+='</p>';
	 hui.pager('service',datas ,"activeList", columnDefine , formatters , buttons ); //分页查询
	 
};

/**
 * 查看代理人活动用户列表
 * @returns
 */
var showQrCodeUser = function(){
	
	var datas = $('#pop_win .data').text();
	var json = eval('('+datas+')');
	if( json )
	{
		window.location.href='page?p=page/qrPromoteActiveUser&name='+json.name+
	    '&agentId='+json.id+'&number='+json.number+'&type='+json.type+
	    "&areaId="+json.areaId;
	}
	else
	{
		common.showMsg('error', '', '查询出错!');
		return;
	}	

};

/**
 * 生成二维码
 * @returns
 */
var createQrCode = function(){
	
	var datas = $('#pop_win .data').text();
	var _json = eval('('+datas+')');
    var datas = {"p":"createAgentHtml","m":"code","id":_json.id,"type":_json.type } ;
    
    common.showProgress();
    
	var json = common.ajaxRequest('service', datas , 'GET');
	if( json && json.result == 0 )
	{
		showAgentList(); //更新显示状态
		$('#pop_win').modal('hide');
		common.hideProgress();
		common.showMsg('info', '', '二维码生成成功!');
	}
	else
	{
		common.showMsg('error', '', '二维码生成失败!' + json.message);
		return;
	}
};

/**
 * 显示新增窗口
 * @returns
 */
var showNewWin = function(){
	$('#newAgentPop input').val('');
	$('#newAgentPop').modal('show');
	
};

/**
 * 显示修改弹出窗口
 * @returns
 */
var showModifyWin = function(){
	var datas = $('#pop_win .data').text();
	var json = eval('('+datas+')');
    $('#modifyAgentPop .agentId').val(json.agentId);
    $('#modifyAgentPop .number').val(json.number);
    $('#modifyAgentPop .oldNumber').val(json.number);
    $('#modifyAgentPop .name').val(json.name);
    $('#modifyAgentPop .address').val(json.address);
    $('#modifyAgentPop .lat').val(json.location_x);
    $('#modifyAgentPop .lng').val(json.location_y);
    $('#modifyAgentPop .areaId').val(json.areaId);
    $('#pop_win').modal('hide');
    $('#modifyAgentPop').modal('show');
	
};

/**
 * 新增代理人
 * @returns
 */
var newAgent = function(){
	
    var number=$('#newAgentPop .number').val();
    var name=$('#newAgentPop .name').val();
    var address=$('#newAgentPop .address').val();
    var location_x=$('#newAgentPop .lat').val();
    var location_y=$('#newAgentPop .lng').val();
    var areaId=$('#newAgentPop .areaId').val();
    
    if( location_x )
    {
    	location_x = parseLocationNumber(location_x);
    }	
    if( location_y )
    {
    	location_y = parseLocationNumber(location_y);
    }	
    
    var datas = {"p":"agentManageOperate","m":"add","number":number,"name":name,"address":address,"location_x":location_x,"location_y":location_y,"areaId":areaId} ;
	var json = common.ajaxRequest('service', datas , 'GET');
	if( json && json.result == 0 )
	{
		showAgentList(); //更新显示状态
		$('#newAgentPop').modal('hide');
		common.showMsg('info', '', '新建成功!');
	}
	else
	{
		common.showMsg('error', '', '新建失败!' + json.message);
		return;
	}
};


/**
 * 修改代理人
 * @returns
 */
var modifyAgent = function(){
	
	var number=$('#modifyAgentPop .number').val();
	var oldNumber=$('#modifyAgentPop .oldNumber').val();
	var name=$('#modifyAgentPop .name').val();
	var address=$('#modifyAgentPop .address').val();
	var location_x=$('#modifyAgentPop .lat').val();
	var location_y=$('#modifyAgentPop .lng').val();
	var areaId=$('#modifyAgentPop .areaId').val();
	var agentId=$('#modifyAgentPop .agentId').val();
	
    if( location_x && location_x.indexOf('.')>0)
    {
    	location_x = parseLocationNumber(location_x);
    }	
    if( location_y && location_y.indexOf('.')>0)
    {
    	location_y = parseLocationNumber(location_y);
    }
	
	var datas = {"p":"agentManageOperate","m":"edit","number":number,"name":name,"address":address,"location_x":location_x,"location_y":location_y,"areaId":areaId,"oldNumber":oldNumber} ;
	var json = common.ajaxRequest('service', datas , 'GET');
	if( json && json.result == 0 )
	{
		showAgentList(); //更新显示状态
		$('#modifyAgentPop').modal('hide');
		common.showMsg('info', '', '编辑成功!');
	}
	else
	{
		common.showMsg('error', '', '编辑失败!' + json.message);
		return;
	}
};




/**
 * 查询地址( 百度地点搜索API)
 * @param q
 * @returns
 */
var queryPlace = function( q , type ,currentDom){
	var a1=$(currentDom).position().left;
	$(".popup").css("left",a1);
	var id = type == 0 ?'newAgentPop':'modifyAgentPop';
	
	if( q && q!='' && q!=null )
	{
		
		var json = common.ajaxRequest('service?p=courierAreaManage&m=queryPlace&query=' +q, null, 'GET');
		if( json && json.result == 0 )
		{
			if( json.addressList && json.addressList.length > 0 )
			{
				$('#'+id +' .popup .con_ul').empty();
				$('#'+id +' .popup').show();
				$(json.addressList).each(function(){
					if( this.location )
					{
						$('#'+id +' .con_ul').append('<li lat="'+this.location.lat+'" lng="'+this.location.lng+'" style="cursor:hand" uid="'+this.uid+'">' + this.name +'</li>');
					}	
				});
			}	
			else
			{
				$('#'+id +' .popup').hide();
			}	

		}
		else
		{
			common.showMsg('info', '', json.message);
		}
		
		$('#'+id +' .con_ul li').click(function(){
		    $('#'+id+' .name').val($(this).text());
		    $('#'+id+' .lat').val($(this).attr('lat'));
		    $('#'+id+' .lng').val($(this).attr('lng'));
		    $('#'+id+' .areaId').val($(this).attr('uid'));
			$('#'+id +' .popup').hide();
		});
	}	
	
};


/**
 * 位置坐标转换
 * @param value
 * @returns
 */
var parseLocationNumber = function( value ){
	var value = value+'';
	var s1 = value.substring(0, value.indexOf('.'));
	var s2 = value.substring(value.indexOf('.')+1);
	if( s2.length < 6 )
	{
		for( var i = 1 ; i<= 6- s2.length; i++ )
		{
			s2+="0";
		}	
	}
	
	return s1+s2;
};

